package com.abc.robot01.fragment

import android.app.Activity
import android.content.Context
import android.net.Uri
import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.compose.foundation.background
import androidx.compose.foundation.gestures.detectTransformGestures
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Slider
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.graphicsLayer
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.platform.ComposeView
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import androidx.compose.ui.viewinterop.AndroidView
import androidx.media3.common.MediaItem
import androidx.media3.common.Player
import androidx.media3.exoplayer.ExoPlayer
import androidx.media3.ui.PlayerView
import com.abc.robot01.R
import com.abc.robot01.databinding.FragmentFirstBinding
import com.bumptech.glide.Glide
import com.bumptech.glide.integration.compose.ExperimentalGlideComposeApi
import com.bumptech.glide.integration.compose.GlideImage
import com.bumptech.glide.load.engine.DiskCacheStrategy
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch


/**
 */
class PicFragment(private val fileUrl: String) : Fragment() {
    val TAG="PicFragment"
    private var _binding: FragmentFirstBinding? = null

    // This property is only valid between onCreateView and
    // onDestroyView.
    private val binding get() = _binding!!

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        Log.e(TAG,"onCreate");
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        return ComposeView(requireContext()).apply {
            setContent {
                MaterialTheme {
                    Surface(modifier = Modifier.fillMaxSize()) {
                            DraggableImage(fileUrl)
                        }
                    }
                }
            }
        }

        // Inflate the layout for this fragment
//        val view = inflater.inflate(R.layout.fragment_pic, container, false)
//        // Find the ImageView
//        val imageView = view.findViewById<ImageView>(R.id.imageView)
//        // Load the image using Glide
//            Glide.with(this)
//                .load(fileUrl)
//                .diskCacheStrategy(DiskCacheStrategy.ALL)
////                .placeholder(R.drawable.placeholder_image) // optional: placeholder while loading
////                .error(R.drawable.error_image) // optional: error image if loading fails
//                .into(imageView)
//        return view
//    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
    }

    override fun onResume() {
        super.onResume()
        Log.e(TAG,"onResume");
    }

    override fun onStart() {
        super.onStart()
        Log.e(TAG,"onStart");
    }

    override fun onPause() {
        super.onPause()
        Log.e(TAG,"onPause");
    }

    override fun onStop() {
        super.onStop()
        Log.e(TAG,"onStop");
    }

    override fun onDestroy() {
        super.onDestroy()
        Log.e(TAG,"onDestroy");
    }

    override fun onDetach() {
        super.onDetach()
        Log.e(TAG,"onDetach");
    }

    override fun onAttach(activity: Activity) {
        super.onAttach(activity)
        Log.e(TAG,"onAttach activity");
    }

    override fun onAttach(context: Context) {
        super.onAttach(context)
        Log.e(TAG,"onAttach Context");
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }
}

@OptIn(ExperimentalGlideComposeApi::class)
@Composable
fun DraggableImage(imageUrl: String) {
    var offset by remember { mutableStateOf(Offset.Zero) }
    var scale by remember { mutableStateOf(1f) }
    Box(
        modifier = Modifier
            .fillMaxSize()
            .pointerInput(Unit) {
                detectTransformGestures { _, pan, zoom, _ ->
                    offset = Offset(
                        offset.x + pan.x,
                        offset.y + pan.y
                    )
                    scale *= zoom
                }
            }
    ) {
        GlideImage(
            model = imageUrl,
            contentDescription ="",
                        modifier = Modifier
                .fillMaxSize()
                .graphicsLayer(
                    translationX = offset.x,
                    translationY = offset.y,
                    scaleX = scale,
                    scaleY = scale
                )
        )
    }
}